package com.fenghongzhang.day019;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private SeekBar seekBar;
    private Button pause;
    private Button restart;
    private Button nav;
    private Button next;
    private Button model;
    private MusicService.MyBinder myBinder;


    private ServiceConnection serviceConnection;
    private int playModel = 0;

    private Timer timer;
    private MusicService musicService;
    private List<Song> music;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        lv = (ListView) findViewById(R.id.lv);
        seekBar = (SeekBar) findViewById(R.id.seek_bar);
        pause = (Button) findViewById(R.id.pause);
        restart = (Button) findViewById(R.id.restart);
        nav = (Button) findViewById(R.id.nav);
        next = (Button) findViewById(R.id.next);
        model = (Button) findViewById(R.id.model);


        //如果当前版本大约6.0
        if(Build.VERSION.SDK_INT >=  Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},888);
        }else{
            start();
        }


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    musicService.setProgress(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(timer != null){
                    timer.cancel();
                }
                //调用服务中的播发方法.
                musicService.play(position);


                //更新进度条的进度
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        seekBar.setMax(musicService.getSeekBarMax());
                        seekBar.setProgress(musicService.getProgress());
                    }
                },0,1000);

            }
        });

    }


//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        if(serviceConnection != null){
//            unbindService(serviceConnection);
////            stopService(intent);
//        }
//    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 888 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            start();
        }
    }

    private void start() {
        //listview的适配器
        music = MusicUtil.getMusic(this);
        final MyAdapter myAdapter = new MyAdapter(this, music);
        lv.setAdapter(myAdapter);
        //开启服务
        intent = new Intent(this, MusicService.class);
        startService(intent);

        serviceConnection = new ServiceConnection() {
           @Override
           public void onServiceConnected(ComponentName name, IBinder service) {
               myBinder = (MusicService.MyBinder) service;
               musicService = myBinder.getService();
           }
           @Override
           public void onServiceDisconnected(ComponentName name) {

           }
       };
        //用bind方式开启
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);
    }

    public void pause(View view) {
        musicService.pause();
    }

    public void restart(View view) {
        musicService.restart();
    }

    public void pre(View view) {
        musicService.pre();
    }

    public void next(View view) {
        musicService.next();
    }

    public void model(View view) {
        final PopupWindow popupWindow = new PopupWindow(this);

        View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);

        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(inflate);

        TextView order = inflate.findViewById(R.id.pop_order);
        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playModel = MusicUtil.ORDER;
                musicService.setPlayModel(playModel);
                Toast.makeText(musicService, "顺序", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });

        TextView random = inflate.findViewById(R.id.pop_random);
        random.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playModel = MusicUtil.RANDOM;
                Toast.makeText(musicService, "随机", Toast.LENGTH_SHORT).show();
                musicService.setPlayModel(playModel);
                popupWindow.dismiss();
            }
        });

        TextView single = inflate.findViewById(R.id.pop_single);
        single.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playModel = MusicUtil.SINGEL;
                musicService.setPlayModel(playModel);
                Toast.makeText(musicService, "单曲", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });


        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(model,0,-350);


    }
}
